package com.lwt.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwt.gulimall.product.entity.Brand;
import com.lwt.gulimall.product.entity.CategoryBrandRelation;
import com.lwt.gulimall.product.mapper.BrandMapper;
import com.lwt.gulimall.product.service.BrandService;
import com.lwt.gulimall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
*
*/
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand>
implements BrandService{

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public Page<Brand> getPage(Map<String, Object> params) {
        long pageSize = Long.parseLong((String)params.get("page"));
        long limit = Long.parseLong((String)params.get("limit"));
        String key = (String)params.get("key");
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(key)){
            wrapper.like(Brand::getBrandId,key)
                    .or()
                    .like(Brand::getName, key);
        }
        Page<Brand> page = new Page<>(pageSize,limit);
        return this.page(page, wrapper);
    }

    @Override
    public void updateDetail(Brand brand) {
        this.updateById(brand);
        categoryBrandRelationService.update(new LambdaUpdateWrapper<CategoryBrandRelation>().eq(CategoryBrandRelation::getBrandId,brand.getBrandId())
                .set(CategoryBrandRelation::getBrandName,brand.getName()));
    }
}
